package com.woniu.tms.interview.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.Interview;
import com.woniu.tms.entity.User;
import com.woniu.tms.interview.service.CompanyService;

@Controller
@RequestMapping("company")
public class FaceTimeController {
	@Autowired
	CompanyService companyService;
	@RequestMapping("unwork")
	@ResponseBody
	public Map<String,Object> unwork(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize,String info) {
		Map<String,Object> map = new HashMap<>();
		try {
			PageHelper.startPage(pageNum, pageSize);
				List<User> list = companyService.finByUnameLike(info);
				PageInfo<User> pageInfo = new PageInfo<User>(list);
				
				map.put("pageInfo", pageInfo);
				map.put("unworkList", list);
			    map.put("result", true);
			  
			
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("everinvite")
	@ResponseBody
	public Map<String,Object> everinvite(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize,String info,HttpSession session) {
		Map<String,Object> map = new HashMap<>();
		try {
			User user = (User) session.getAttribute("loginUser");
			PageHelper.startPage(pageNum, pageSize);
				List<Interview> list = companyService.finByUnameLike3(info,user);
				PageInfo<Interview> pageInfo = new PageInfo<Interview>(list);
				
				map.put("pageInfo", pageInfo);
				map.put("everinviteList", list);
			    map.put("result", true);
			  
			
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("addandinvite")
	@ResponseBody
	public Map<String,Object> addandinvite(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize,String info,HttpSession session) {
		Map<String,Object> map = new HashMap<>();
		try {
			User user = (User) session.getAttribute("loginUser");
			PageHelper.startPage(pageNum, pageSize);
				List<Interview> list = companyService.finByUnameLike4(info,user);
				PageInfo<Interview> pageInfo = new PageInfo<Interview>(list);
				
				map.put("pageInfo", pageInfo);
				map.put("everinviteList", list);
			    map.put("result", true);
			  
			
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("sendAll")
	@ResponseBody
	public Map<String,Object> sendAll(Integer[] uid,HttpSession session){
		Map<String,Object> map = new HashMap<>();
		try {
			User loginUser = (User) session.getAttribute("loginUser");
			for (Integer id : uid) {
				
				User user =companyService.findOne(id);
				companyService.add(user,loginUser);
				companyService.sendMail("1012847957@qq.com", "kuvrnreyzwrwbahh", "smtp.qq.com", "1012847957@qq.com", user.getUeamail(), "面试邀请", "您有一封来自"+loginUser.getUname()+"公司的面试邀请，请尽快与公司联系联系方式为"+loginUser.getUphone());
			}
			map.put("result", true);
			
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("sendOne")
	@ResponseBody
	public Map<String,Object> sendOne(Integer uid,HttpSession session){
		Map<String,Object>map=new HashMap<>();
		try {
			User loginUser = (User) session.getAttribute("loginUser");
			User user =companyService.findOne(uid);
			companyService.add(user,loginUser);
			companyService.sendMail("1012847957@qq.com", "kuvrnreyzwrwbahh", "smtp.qq.com", "1012847957@qq.com", user.getUeamail(), "面试邀请", "您有一封来自"+loginUser.getUname()+"公司的面试邀请，请尽快与公司联系联系方式为"+loginUser.getUphone());
		    map.put("result", true);
		}catch(Exception e) {
			 map.put("result", false);
		}
		
		return map;
	}
	@RequestMapping("studentFaceTimeindex")
	@ResponseBody
	public Map<String,Object> studentFaceTimeindex(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize,String info,HttpSession session) {
		Map<String,Object> map = new HashMap<>();
		try {
			User loginUser = (User) session.getAttribute("loginUser");
			PageHelper.startPage(pageNum, pageSize);
				List<Interview> list = companyService.findByUnameLike(info,loginUser);
				PageInfo<Interview> pageInfo = new PageInfo<Interview>(list);
				map.put("pageInfo", pageInfo);
				map.put("interviewResultList", list);
			    map.put("result", true);
			  
			
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("teacherwithresultindex")
	@ResponseBody
	public Map<String,Object> teacherwithresultindex(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize,String info,HttpSession session) {
		Map<String,Object> map = new HashMap<>();
		try {
			User loginUser = (User) session.getAttribute("loginUser");
			PageHelper.startPage(pageNum, pageSize);
				List<Interview> list = companyService.findLikeUname(info,loginUser);
				PageInfo<Interview> pageInfo = new PageInfo<Interview>(list);
				map.put("pageInfo", pageInfo);
				map.put("interviewResultList", list);
			    map.put("result", true);
			  
			
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("directorlookindex")
	@ResponseBody
	public Map<String,Object> directorlookindex(String className,@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize,String info,HttpSession session) {
		Map<String,Object> map = new HashMap<>();
		System.out.println(className);
		try {
			User loginUser = (User) session.getAttribute("loginUser");
			PageHelper.startPage(pageNum, pageSize);
				List<Interview> list = companyService.findLikeUname2(info,loginUser,className);
				PageInfo<Interview> pageInfo = new PageInfo<Interview>(list);
				map.put("pageInfo", pageInfo);
				map.put("interviewResultList", list);
			    map.put("result", true);
			  	
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("init")
	@ResponseBody
	public Map<String,Object> init(){
		Map<String,Object> map = new HashMap<>();
		try {
			List<Classes> list2 = companyService.finAllClasses();
			map.put("Classes",list2);
			map.put("result", true);
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}
	@RequestMapping("saverecive")
	@ResponseBody
	public Map<String,Object> saverecive(String review,Integer studentid){
		Map<String,Object> map = new HashMap<>();
		try {
			companyService.saverecive(review,studentid);
			map.put("result", true);
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
		
	}
	@RequestMapping("savetips")
	@ResponseBody
	public Map<String,Object> savetips(String tips,Integer iid){
		Map<String,Object> map = new HashMap<>();
		try {
			companyService.savetips(tips,iid);
			map.put("result", true);
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
		
	}
	@RequestMapping("pass")
	@ResponseBody
	public Map<String,Object> pass(Interview interview){
		Map<String,Object> map= new HashMap<>();
		try {
			companyService.updateOne(interview);
			map.put("result",true);
		}catch(Exception e) {
			map.put("result",false);
		}
		return map;
		
	}
	@RequestMapping("studentFaceTime")
	public String studentFaceTime() {
		
		return "WEB-INF/interview/studentin/studentFaceTime";
	}
	@RequestMapping("addandinviteindex")
	public String addandinviteindex() {
		
		return "WEB-INF/interview/empinterview/addandinviteindex";
	}
	@RequestMapping("everinviteindex")
	public String everinviteindex() {
		
		return "WEB-INF/interview/companyInterview/everinvite";
	}
	@RequestMapping("teacherlookforface")
	public String teacherlookforface() {
		
		return "WEB-INF/interview/classmanagerin/classwithfacetime";
	}
	@RequestMapping("directorlookforface")
	public String directorlookforface() {
		
		return "WEB-INF/interview/Directorin/directorlook";
	}
	@RequestMapping("teacherjurdge")
	public String teacherjurdge() {
		
		return "WEB-INF/interview/teacherin/teacherjurdge";
	}
	@RequestMapping("unworkindex")
	public String unworkindex() {
		
		return "WEB-INF/interview/companyInterview/unworkindex";
	}

}
